package com.xpec.c4.h5net;

import java.math.BigDecimal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;

import com.google.gson.Gson;
import com.xpec.c4.message.login.CAccountLoginRequest;

public class H5Coder {
	private static final Log log = LogFactory.getLog(H5Coder.class);

	public static int getMessageTypeJSON(JSONObject decodeString) {
		int messageType = 0;
		try {
			messageType = (Integer) decodeString.get("messageType");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			if (log.isErrorEnabled()) {
				log.error("cannot get messageType, message=" + decodeString);
			}
		}
		return messageType;
	}

	public static String getModuleIdJSON(JSONObject decodeString) {
		String moduleId = null;
		try {
			moduleId = (String) decodeString.get("moduleId");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			if (log.isErrorEnabled()) {
				log.error("cannot get moduleId, message=" + decodeString);
			}
		}
		return moduleId;
	}

	// public CAccountLoginRequest loginRequest(JSONObject decodeString) {
	// CAccountLoginRequest request = new CAccountLoginRequest();
	// try {
	// request.clientIp = (String) decodeString.get("clientIp");
	// request.code = (String) decodeString.get("code");
	// request.login = (String) decodeString.get("login");
	// request.loginPlatForm = (String) decodeString.get("loginPlatForm");
	// request.reconnectToken = (String) decodeString.get("reconnectToken");
	// } catch (JSONException e) {
	// // TODO Auto-generated catch block
	// e.printStackTrace();
	// }
	// return request;
	// }

//	public static Object jsonToClass(JSONObject jsonObject, Class objClass) {
//		Gson gson = new Gson();
//		return gson.fromJson(jsonObject.toString(), objClass);
//	}

	public static Object jsonStringToClass(String jsonString, Class objClass) {
		Gson gson = new Gson();
		Object jsonToClass = new Object();
		switch (objClass.getSimpleName()) {
		case "Boolean":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = (Boolean) jsonObject.get("value");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Byte":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = (Byte) jsonObject.get("value");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Character":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = (Character) jsonObject.get("value");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Short":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = Short.valueOf((String) jsonObject.get("value"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Integer":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = Integer.valueOf((String) jsonObject.get("value"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Long":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = Long.valueOf((String) jsonObject.get("value"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Float":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = Float.valueOf((String) jsonObject.get("value"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "Double":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = Double.valueOf((String) jsonObject.get("value"));
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "String":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = (String)jsonObject.get("value");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		case "BigDecimal":
			try {
				JSONObject jsonObject = new JSONObject(jsonString);
				jsonToClass = (BigDecimal)jsonObject.get("value");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			break;
		default:
			try {
				jsonToClass = gson.fromJson(jsonString, objClass);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error("json parse fail, class: " + objClass.getName() + " json: " + jsonString, e);
				}
				jsonToClass = null; 
			}
			break;
		}
		return jsonToClass;
	}

	public static JSONObject classToJson(Object object) {
		Gson gson = new Gson();
		String gsonObject = gson.toJson(object);
		String typeName = object.getClass().getSimpleName();
		JSONObject jsonObject = new JSONObject();
		try {
			switch (typeName) {
			case "Boolean":
			case "Byte":
			case "Character":
			case "Short":
			case "Integer":
			case "Long":
			case "Float":
			case "Double":
			case "String":
			case "BigDecimal":
				jsonObject.put("value", gsonObject);
				break;
			default:
				jsonObject = new JSONObject(gsonObject);
				break;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonObject;
	}

	public static String classToJsonString(Object object) {
		Gson gson = new Gson();
		return gson.toJson(object);
	}

	public static void main(String[] args) throws Exception {
		// CAccountLoginRequest request = new CAccountLoginRequest();
		// request.clientIp = "192.168.50.183";
		// request.code = "123";
		// request.login = "login_1";
		// request.reconnectToken = "789";
		// request.loginPlatForm = "windows";
		// classToJson(request);

		JSONObject jObject = new JSONObject(
				"{'code':'9530','messageType':'101002','clientIp':'192.168.50.183','login':'login_1','moduleId':'login_1','reconnectToken':'e12078eb30ddab72ceb74750167186b29167c21a'}");
		// JSONObject jObject = new
		// JSONObject("{'code':'9530','clientIp':'192.168.50.183','reconnectToken':'e12078eb30ddab72ceb74750167186b29167c21a','login':'login_1'}");
		// JSONObject jObject = new
		// JSONObject("{'code':'123','caid':'456','clientIp':'789','reconnectToken':'987','login':'654'}");
		jsonStringToClass(jObject.toString(), CAccountLoginRequest.class);
		// Gson gson = new Gson();
		// gson.fromJson(jObject.toString(), CAccountLoginRequest.class);
	}
}